package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    AddressBookService addressBookService;
    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R addShopp(@RequestBody ShoppingCart shoppingCart) {
        Long userId = BaseContext.getCurrentId();
        /* 菜品或者套餐是否点过
     如何知道烧鹅被点过了？？ 根据烧鹅的菜品id，查询下数据库中有没有烧鹅这个数据
     如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
     如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
     */
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.lambda().eq(ShoppingCart::getUserId, userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            qw.lambda().eq(ShoppingCart::getDishId, dishId);
        } else {
            qw.lambda().eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one == null) {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        } else {
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }

    @GetMapping("/list")
    public R getLista() {
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        qw.lambda().eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R subShopp(@RequestBody ShoppingCart shoppingCart) {
        //拿到userid
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.lambda().eq(ShoppingCart::getUserId, userId);
        //通过传来的购物车数据拿到菜品的id
        Long dishId = shoppingCart.getDishId();
        //判断菜品id有没有 没有的话一定是套餐
        if (dishId != null) {
            //购物车加where条件比对菜品
            qw.lambda().eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //购物车加where条件比对套餐
            qw.lambda().eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //经过上面的比对这里肯定有套餐或者菜品数据
        ShoppingCart one = shoppingCartService.getOne(qw);
        //拿到套餐或者菜品的num数量
        Integer number = one.getNumber();
        //进行-- 减少购物车数量
        //如果大于0修改 小于0直接删除数据库数据
        number--;
        //把num设置进去 不设置白修改了
        one.setNumber(number);
        if (number > 0) {
           // shoppingCart.setNumber(number--);
            //one=shoppingCart;
            shoppingCartService.updateById(one);
        } else {
            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R delete() {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.lambda().eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(qw);
        return R.success("清空购物车成功");
    }

}
